package com.bawei.mall.product.controller;

import com.bawei.common.core.domain.R;
import com.bawei.common.core.utils.poi.ExcelUtil;
import com.bawei.common.core.web.controller.BaseController;
import com.bawei.common.core.web.domain.AjaxResult;
import com.bawei.common.log.annotation.Log;
import com.bawei.common.log.enums.BusinessType;
import com.bawei.common.security.annotation.RequiresPermissions;
import com.bawei.mall.product.domain.*;
import com.bawei.mall.product.service.IMallProductBrandInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品品牌Controller
 *
 * @author DongZeLiang
 * @date 2022-09-15
 */
@RestController
@RequestMapping("/brand")
public class MallProductBrandInfoController extends BaseController
{
    @Autowired
    private IMallProductBrandInfoService mallProductBrandInfoService;

    /**
     * 查询商品品牌列表
     */
    @RequiresPermissions("product:brand:list")
    @GetMapping("/list")
    public R<List<MallProductBrandInfo>> list(MallProductBrandInfo mallProductBrandInfo) {

        PageHelper.startPage(mallProductBrandInfo.getPageNum(), mallProductBrandInfo.getPageSize());
        List<MallProductBrandInfo> list = mallProductBrandInfoService.selectMallProductBrandInfoLists(mallProductBrandInfo);
        PageInfo<MallProductBrandInfo> mallProductBrandInfoPageInfo = new PageInfo<>(list);
        return R.ok(mallProductBrandInfoPageInfo.getList());
    }

    @GetMapping("/content")
    public R<MallProductList> advertise() {

        MallProductList mallProductList = new MallProductList();
        MallProductBrandInfo mallProductBrandInfo = new MallProductBrandInfo();
        List<MallProductBrandInfo> mallProductBrandInfos = mallProductBrandInfoService.selectMallProductBrandInfoLists(mallProductBrandInfo);

        List<MallProductAdvertise> mallProductAdvertises = mallProductBrandInfoService.selectMallProductAdvertise();


        List<MallProductInfo> homeFlashPromotion = mallProductBrandInfoService.
                selectMallProductInfo(1,0,0);

        List<MallProductInfo> hotProductList = mallProductBrandInfoService.
                selectMallProductInfo(0,1,0);

        List<MallProductInfo> newProductList = mallProductBrandInfoService.
                selectMallProductInfo(0,0,1);


        mallProductList.setMallProductBrandInfos(mallProductBrandInfos);
        mallProductList.setMallProductAdvertises(mallProductAdvertises);
        mallProductList.setHomeFlashPromotion(homeFlashPromotion);
        mallProductList.setHotProductList(hotProductList);
        mallProductList.setNewProductList(newProductList);


        return R.ok(mallProductList);
    }

    @GetMapping("/brandInfoList")
    public R<List<MallProductBrandInfo>> Advertise(@RequestParam Integer pageNum,@RequestParam Integer pageSize){
        MallProductBrandInfo mallProductBrandInfo = new MallProductBrandInfo();
        mallProductBrandInfo.setPageNum(pageNum);
        mallProductBrandInfo.setPageSize(pageSize);
        PageHelper.startPage(mallProductBrandInfo.getPageNum(), mallProductBrandInfo.getPageSize());
        List<MallProductBrandInfo> mallProductAdvertises = mallProductBrandInfoService.selectMallProductBrandAdvertise(mallProductBrandInfo);
        PageInfo<MallProductBrandInfo> mallProductBrandInfoPageInfo = new PageInfo<>(mallProductAdvertises);
        return R.ok(mallProductBrandInfoPageInfo.getList());
    }

    /**
     * 导出商品品牌列表
     */
    @RequiresPermissions("product:brand:export")
    @Log(title = "商品品牌", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallProductBrandInfo mallProductBrandInfo)
    {
        List<MallProductBrandInfo> list = mallProductBrandInfoService.selectMallProductBrandInfoList(mallProductBrandInfo);
        ExcelUtil<MallProductBrandInfo> util = new ExcelUtil<MallProductBrandInfo>(MallProductBrandInfo.class);
        util.exportExcel(response, list, "商品品牌数据");
    }

     /*获取商品品牌详细信息*/
    @RequiresPermissions("product:brand:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(mallProductBrandInfoService.selectMallProductBrandInfoById(id));
    }


    /**
     * 根据品牌id查询商品品牌信息
     * @param id
     * @return
     */

    @GetMapping("/detail/{id}")
    public AjaxResult getBrandInfo(@PathVariable("id") Long id) {
        AjaxResult success = AjaxResult.success(mallProductBrandInfoService.selectMallProductBrandInfoById(id));
        return success;
    }

    /**
     * 查询商品收藏
     * @param brandId
     * @return
     */
    @GetMapping("/details")
    public R<List<MallProductCollect>> getBrandInfos(@RequestParam Long brandId) {
        List<MallProductCollect> success = mallProductBrandInfoService.selectMallProductBrandInfos(brandId);
        if(success.size()>0){
            return R.ok(success);
        }
        return null;
    }


    /**
     * 添加商品收藏
     * @param mallProductCollect
     * @return
     */
    @PostMapping("/add")
    public R AddCollect(@RequestBody MallProductCollect mallProductCollect){
        mallProductBrandInfoService.AddMallProductCollect(mallProductCollect);
        return R.ok();
    }


    /**
     * 删除商品收藏
     * @param brandId
     * @return
     */
    @PostMapping("/delete")
    public R deleteCollect(@RequestParam String brandId){
        mallProductBrandInfoService.deleteMallProductCollect(brandId);
        return R.ok();
    }


    /**
     * 新增商品品牌
     */
    @RequiresPermissions("product:brand:add")
    @Log(title = "商品品牌", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallProductBrandInfo mallProductBrandInfo)
    {
        return toAjax(mallProductBrandInfoService.insertMallProductBrandInfo(mallProductBrandInfo));
    }

    /**
     * 修改商品品牌
     */
    @RequiresPermissions("product:brand:edit")
    @Log(title = "商品品牌", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallProductBrandInfo mallProductBrandInfo)
    {
        return toAjax(mallProductBrandInfoService.updateMallProductBrandInfo(mallProductBrandInfo));
    }

    /**
     * 删除商品品牌
     */
    @RequiresPermissions("product:brand:remove")
    @Log(title = "商品品牌", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(mallProductBrandInfoService.deleteMallProductBrandInfoByIds(ids));
    }

    @PostMapping("/clear")
    public AjaxResult clear() {
        mallProductBrandInfoService.deleteMallProductBrandInfo();
        return success();
    }
}
